<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class SignController extends Controller
	{
		/* 
		 *签到 
		 *  
		 */
		public function signIn($uid=null, $token=null)
		{
// 			if ($this->checkedToken($uid, $token)) 
// 			{
				$where["uid"] = $uid;
				$signInfo = M("Sign") -> field("signday,signdays,signtime") -> where($where) -> find();
				//var_dump($signInfo);
				if ($signInfo == null) 
				{
					//第一次签到
					$data["uid"] = $uid;
					$data["signday"] = 1;
					$data["signtime"] = date("Y-m-d H:i:s",time());
					$data["signdays"] = date("d", time());
					
					$result = M("Sign") -> data($data) ->add();
					if ($result > 0) 
					{
						M("User") -> where("uid={$uid}") ->setInc("credit", 10);
						M("Sign") -> where("uid={$uid}") ->setInc("signtimes", 1);
						$array1['result'] = 0;
						$array1['message'] = "签到成功";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 10601;
						$array1['message'] = "签到失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				else
				{
					//不是第一次的签到
					//$time =  time()-$signInfo["signtime"];
					
					//获得签到当日凌晨的时间戳  
  					$date = substr($signInfo["signtime"], 0, 10);
					
					$signToday = strtotime($date."24:00:00",time()); 
					//距离当天的零点的时间
					$hour = ($signToday - strtotime($signInfo["signtime"], time()))/3600;

					$time =  time()-$signToday;
					
					//var_dump($date);
					//var_dump($hour);
					//已签到的情况
					if ( $time/3600 < $hour) 
					{
						//距离上一次签到还在同一天
						$array1['result'] = 10602;
						$array1['message'] = "今天已签到";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					elseif ($time/3600 > 24)
					{
						//距离上一次签到超过二十四个小时
						$daysArray11 = explode(",", $signInfo["signdays"]);
						$d11 = date("d", time());
						array_push($daysArray11, $d11);
						$daystring11 = implode(",", $daysArray11);
						
						//需要更新的数据
						$data1 = array(
								"signday"=>1,
								"signdays"=>$daystring11,
								"signtime"=>date("Y-m-d H:i:s", time())
						); 
						$result = M("Sign") -> where($where) -> data($data1) ->save();
						if (condition) 
						{
							$array1['result'] = 0;
							$array1['message'] = "签到成功";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							$array1['result'] = 10603;
							$array1['message'] = "签到失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						
					}
					else
					{
						//需加连续签到天数判断
						$d = date("d", time());
						$daysArray = explode(",", $signInfo["signdays"]);
						if (in_array($d, $daysArray)) 
						{
							$array1['result'] = 10602;
							$array1['message'] = "今天已签到";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							array_push($daysArray, $d);
							$daystring = implode(",", $daysArray);
						}
						$result = M("Sign") -> where($where) -> setField("signdays",$daystring);
						if ($result > 0) 
						{
							$res = M("Sign") -> where($where) ->setInc("signday", 1);
						}
						else
						{
							$array1['result'] = 10604;
							$array1['message'] = "签到失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						
						
						if ($res > 0) 
						{
							$res1 = M("User") -> where($where) ->setInc("credit",10);
							
							if ($res1 > 0) 
							{
								$array1['result'] = 0;
								$array1['message'] = "签到成功";
								echo json_encode($array1, JSON_UNESCAPED_SLASHES);
								exit;
							}
							
						}
						else
						{
							$array1['result'] = 10605;
							$array1['message'] = "签到失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
					}
				}
// 			}
// 			else
// 			{
// 				$array1['result'] = 10310;
// 			 	$array1['message'] = "获取视频失败";
// 			 	echo json_encode($array1, JSON_UNESCAPED_SLASHES);
// 			 	exit;
// 			}
		}
		
		/*
		 *验证token的方法
		 *
		 */
		protected function checkedToken($uid = null,$token = null)
		{
			if ($uid==null || $token==null)
			{
				return false;
			}
			else
			{
				$where["uid"] = $uid;
				$tokeninfo = M("User") -> where($where) -> getField("token");
				if ($token == $tokeninfo)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}